package cn.iocoder.yudao.module.system.controller.admin.companyjob;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.companyjob.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.companyjob.CompanyJobDO;
import cn.iocoder.yudao.module.system.service.companyjob.CompanyJobService;

@Tag(name = "管理后台 - 公司职位")
@RestController
@RequestMapping("/system/company-job")
@Validated
public class CompanyJobController {

    @Resource
    private CompanyJobService companyJobService;

    @PostMapping("/create")
    @Operation(summary = "创建公司职位")
    @PreAuthorize("@ss.hasPermission('system:company-job:create')")
    public CommonResult<Long> createCompanyJob(@Valid @RequestBody CompanyJobSaveReqVO createReqVO) {
        return success(companyJobService.createCompanyJob(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新公司职位")
    @PreAuthorize("@ss.hasPermission('system:company-job:update')")
    public CommonResult<Boolean> updateCompanyJob(@Valid @RequestBody CompanyJobSaveReqVO updateReqVO) {
        companyJobService.updateCompanyJob(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除公司职位")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:company-job:delete')")
    public CommonResult<Boolean> deleteCompanyJob(@RequestParam("id") Long id) {
        companyJobService.deleteCompanyJob(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得公司职位")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:company-job:query')")
    public CommonResult<CompanyJobRespVO> getCompanyJob(@RequestParam("id") Long id) {
        CompanyJobDO companyJob = companyJobService.getCompanyJob(id);
        return success(BeanUtils.toBean(companyJob, CompanyJobRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得公司职位分页")
    @PreAuthorize("@ss.hasPermission('system:company-job:query')")
    public CommonResult<PageResult<CompanyJobRespVO>> getCompanyJobPage(@Valid CompanyJobPageReqVO pageReqVO) {
        PageResult<CompanyJobDO> pageResult = companyJobService.getCompanyJobPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CompanyJobRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出公司职位 Excel")
    @PreAuthorize("@ss.hasPermission('system:company-job:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCompanyJobExcel(@Valid CompanyJobPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CompanyJobDO> list = companyJobService.getCompanyJobPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "公司职位.xls", "数据", CompanyJobRespVO.class,
                        BeanUtils.toBean(list, CompanyJobRespVO.class));
    }

}